package simple.number;

/**
 * <a href="https://leetcode.cn/problems/number-complement/">476. 数字的补数</a>
 * <a href="https://leetcode.cn/problems/complement-of-base-10-integer/">1009. 十进制整数的反码</a>
 * @author 刘学松
 * @date 2023-05-16 9:23
 */
public class 数字的补数_十进制整数的反码 {
    public int bitwiseComplement(int n) {
        String s = Integer.toBinaryString(n);
        int result = 0;
        char[] chars = s.toCharArray();
        for (char c : chars) {
            if (c=='0') {
                result = (result << 1) + 1;
            } else {
                result = (result << 1);
            }
        }
        return result;
    }
}
